package com.youi.util;

import com.youi.model.Coordinate;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;


/**
 * 对象工具类
 *
 * @param <T>
 * @author erdong.Chan
 */
public class ObjectUtil {

    /**
     * 判断对象所有String属性是否为null，如果是设置为""字符串
     *
     * @param obj
     */
    public static void ToChangeNullToEmpty(Object obj) {
        Class cls = obj.getClass();
        Field[] fields = cls.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String name = field.getName();
            if (field.getType().equals(String.class)) {    //如果为字符串类型
                String upperName = name.substring(0, 1).toUpperCase() + name.substring(1, name.length());//获取大写开头变量名
                try {
                    if (cls.getDeclaredMethod("get" + upperName).invoke(obj) == null) {    //判断是否为空  如果为空设值为""
                        cls.getDeclaredMethod("set" + upperName, String.class).invoke(obj, "");
                    }
                } catch (IllegalAccessException | IllegalArgumentException
                        | InvocationTargetException | NoSuchMethodException
                        | SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 判断对象所有String属性是否为null
     *
     * @param obj
     */
    public static boolean checkAllPropertyIsEmpty(Object obj) {
        Class cls = obj.getClass();
        Field[] fields = cls.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String name = field.getName();
            if (field.getType().equals(String.class)) {    //如果为字符串类型
                String upperName = name.substring(0, 1).toUpperCase() + name.substring(1, name.length());//获取大写开头变量名
                try {
                    if (cls.getDeclaredMethod("get" + upperName).invoke(obj) != null && !cls.getDeclaredMethod("get" + upperName).invoke(obj).equals("")) {    //判断不等于空 且不等于""
                        return false;
                    }
                } catch (IllegalAccessException | IllegalArgumentException
                        | InvocationTargetException | NoSuchMethodException
                        | SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

    /**
     * list转string逗号隔开
     *
     * @param strList
     * @return
     */
    public static String listToString(List<String> strList) {
        if (strList != null && strList.size() > 0) {
            String strs = StringUtils.strip(strList.toString(), "[]");
            String result = strs.replace(" ", "");
            return result;
        }
        return null;
    }

    /**
     * list去重复
     **/
    public static List removeDuplicate(List arlList) {
        HashSet h = new HashSet(arlList);
        arlList.clear();
        arlList.addAll(h);
        return arlList;
    }

    /**
     * 精度计算 返回整形
     *
     * @param v1
     * @param v2
     * @return
     */
    public static Integer Intgermul(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.multiply(b2).intValue();
    }

    public static String FormatFloat(Float flo) {
        DecimalFormat decimalFormat = new DecimalFormat(".00");
        return decimalFormat.format(flo);
    }

    public static String FormatBouble(Double dou) {
        DecimalFormat decimalFormat = new DecimalFormat(".00");
        return decimalFormat.format(dou);
    }

    /**
     * 两数相乘
     *
     * @param v1
     * @param v2
     * @return
     */
    public static Integer mul(Integer v1, Float v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.multiply(b2).intValue();
    }

    /**
     * 两数相乘
     *
     * @param v1
     * @param v2
     * @return
     */
    public static Float mulGetFloat(Integer v1, Float v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.multiply(b2).floatValue();
    }

    public static String mulGetString(Integer v1, Float v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.multiply(b2).floatValue() + "";
    }

    /**
     * 相除
     */
    public static Integer divide(Integer v1, Integer v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.divide(b2, 2, RoundingMode.HALF_UP).intValue();
    }

    /**
     * 相除
     */
    public static Float divide(Float v1, Integer v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2 + "");
        return b1.divide(b2, 2, RoundingMode.HALF_UP).floatValue();
    }

    /**
     * 根据ids字符串获取list
     *
     * @param ids
     * @return
     */
    public static List<Long> getIdListByIds(String ids) {
        List<Long> idsList = null;
        if (ids != null && ids.length() > 0) {
            String[] idsArr = ids.split(",");
            if (idsArr != null && idsArr.length > 0) {
                idsList = new ArrayList<Long>();
                for (String id : idsArr) {
                    idsList.add(Long.valueOf(id));
                }
            }
        }
        return idsList;
    }

    /**
     * 根据strs字符串获取list
     *
     * @param ids
     * @return
     */
    public static List<String> getStrListByIds(String strs) {
        List<String> strList = null;
        if (strs != null && strs.length() > 0) {
            String[] strArr = strs.split(",");
            if (strArr != null && strArr.length > 0) {
                strList = new ArrayList<String>();
                for (String str : strArr) {
                    strList.add(str);
                }
            }
        }
        return strList;
    }

    /**
     * 根据strs字符串获取list
     *
     * @param ids
     * @return
     */
    public static List<Integer> getIntListByIds(String strs) {
        List<Integer> intList = null;
        if (strs != null && strs.length() > 0) {
            String[] strArr = strs.split(",");
            if (strArr != null && strArr.length > 0) {
                intList = new ArrayList<Integer>();
                for (String str : strArr) {
                    intList.add(Integer.valueOf(str));
                }
            }
        }
        return intList;
    }

    /**
     * 获取性别描述
     *
     * @param gender
     * @return
     */
    public static String getGenderDesc(Integer gender) {
        if (gender != null) {
            switch ((int) gender) {
                case 0:
                    return "女";
                case 1:
                    return "男";
            }
        }
        return "未知";
    }

    /**
     * 获取用户身份
     * 商户(0否 1普通商家 2兼职商家)
     *
     * @return
     */
    public static String getBusiness(Integer business) {
        if (business != null) {
            switch ((int) business) {
                case 0:
                    return "否";
                case 1:
                    return "普通商家";
                case 2:
                    return "兼职商家";
            }
        }
        return "未知";
    }

    /**
     * 获取性别描述
     *
     * @param gender
     * @return
     */
    public static String getCheckStaus(Integer status) {
        if (status != null) {
            switch ((int) status) {
                case -1:
                    return "不通过";
                case 0:
                    return "待审核";
                case 1:
                    return "已通过";
            }
        }
        return "";
    }

    /**
     * @param state 订单状态(-1已关闭 0待付款 1待发货 2待收货 3待评价 4已完成 5售后)
     * @return
     */
    public static String getOrderState(Integer state) {
        if (state != null) {
            switch ((int) state) {
                case -1:
                    return "已关闭";
                case 0:
                    return "待付款";
                case 1:
                    return "待发货";
                case 2:
                    return "待收货";
                case 3:
                    return "待评价";
                case 4:
                    return "已完成";
                case 5:
                    return "售后";
            }
        }
        return "";
    }

    /**
     * 精度计算 返回double类型
     *
     * @param v1
     * @param v2
     * @return
     */
    public static double Doublemul(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.multiply(b2).doubleValue();
    }

    /**
     * 获取提现状态(-1 未处理 0已处理)
     *
     * @param state
     * @return
     */
    public static String getWithdrawalState(Integer state) {
        if (state != null) {
            switch ((int) state) {
                case -1:
                    return "未支付";
                case 0:
                    return "已处理";
            }
        }
        return "";
    }

    /**
     * 获取计价单位
     *
     * @param gender
     * @return
     */
    public static String getPriceUnit(Integer priceType) {
        if (priceType != null) {
            switch ((int) priceType) {
                case 0:
                    return "小时";
                case 1:
                    return "天";
            }
        }
        return "未知";
    }

    /**
     * 获取服务状态
     *
     * @param gender
     * @return
     */
    public static String getServicesState(Integer state) {
        if (state != null) {
            switch ((int) state) {
                case 0:
                    return "已下架";
                case 1:
                    return "在售";
            }
        }
        return "未知";
    }

    /**
     * 获取支付方式
     *
     * @param payType//支付方式(0余额支付 1银行卡支付 2微信支付 )
     * @return
     */
    public static String getPayType(Integer payType) {
        if (payType != null) {
            switch ((int) payType) {
                case -1:
                    return "未支付";
                case 0:
                    return "余额支付";
                case 1:
                    return "银行卡支付";
                case 2:
                    return "微信支付";
                case 3:
                    return "支付宝支付";
            }
        }
        return "未知";
    }

    /**
     * 获取易宝支付方式
     *
     * @param payType 支付方式(0余额支付 1银行卡支付 2微信支付 )
     * @return
     */
    public static String getYibaoPayType(Integer payType) {
        if (payType != null) {
            switch ((int) payType) {
                case 0:
                    return "余额支付";
                case 1:
                    return "UPOP";
                case 2:
                    return "WECHAT";
                case 3:
                    return "UPOP";
            }
        }
        return "未知";
    }

    /**
     * 获取是否
     *
     * @param gender
     * @return
     */
    public static String getIf(Integer state) {
        if (state != null) {
            switch ((int) state) {
                case 0:
                    return "否";
                case 1:
                    return "是";
            }
        }
        return "未知";
    }

    /**
     * 计算最少坐标和最大坐标
     *
     * @param latitude
     * @param longitude
     * @return
     */
    public static Coordinate getCoordinate(Double latitude, Double longitude) {
        if (latitude != null && longitude != null) {
            double r = 6371;//地球半径千米
            double dis = 1.0;//千米距离
            double dlng = 2 * Math.asin(Math.sin(dis / (2 * r)) / Math.cos(latitude * Math.PI / 180));
            dlng = dlng * 180 / Math.PI;//角度转为弧度
            double dlat = dis / r;
            dlat = dlat * 180 / Math.PI;
            double minlat = latitude - dlat;
            double maxlat = latitude + dlat;
            double minlng = longitude - dlng;
            double maxlng = longitude + dlng;
            Coordinate coordinate = new Coordinate(minlat, maxlat, minlng, maxlng);
            return coordinate;
        }
        return null;
    }

    /**
     * 补齐数字
     *
     * @param count
     * @return
     */
    public static String getCode(String str) {
        String code = null;
        switch (str.length()) {
            case 1:
                code = "00000" + str;
                break;
            case 2:
                code = "0000" + str;
                break;
            case 3:
                code = "000" + str;
                break;
            case 4:
                code = "00" + str;
                break;
            case 5:
                code = "0" + str;
                break;
            case 6:
                code = str;
                break;
            default:
                code = "999999";
                break;
        }
        return code;
    }

    /**
     * 去除字符串中所包含的空格
     *
     * @param s
     * @return
     */
    public static String removeAllBlank(String s) {
        String result = "";
        if (null != s && !"".equals(s)) {
            result = s.replaceAll(" ", "");
        }
        return result;
    }

    public static String getOrderNo(Integer type, String id) {
        if (type != null && id != null && id.trim().length() > 0) {
            String orderNo = type.toString() + getCode(id) + new Date().getTime();
            return orderNo;
        }
        return "5999999" + new Date().getTime();
    }

    /**
     * @param value 需要科学计算的数据
     * @param digit 保留的小数位
     * @return 功能：四舍六入五成双计算法
     */
    public static Double sciCal(double value, int digit) {
        String result = "-999";
        try {
            double ratio = Math.pow(10, digit);
            double _num = value * ratio;
            double mod = _num % 1;
            double integer = Math.floor(_num);
            double returnNum;
            if (mod > 0.5) {
                returnNum = (integer + 1) / ratio;
            } else if (mod < 0.5) {
                returnNum = integer / ratio;
            } else {
                returnNum = (integer % 2 == 0 ? integer : integer + 1) / ratio;
            }
            BigDecimal bg = new BigDecimal(returnNum);
            result = bg.setScale((int) digit, BigDecimal.ROUND_HALF_UP).toString();
        } catch (RuntimeException e) {
            throw e;
        }
        return Double.valueOf(result);
    }

}
